package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entry.*;
import com.example.demo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Wrapper;
import java.util.List;

@RestController("/user")
public class UserController {

    @Autowired
    private UserAccountService userAccountService;
    @Autowired
    private RecommentcarService recommentcarService;
    @Autowired
    private CommguildService commguildService;
    @Autowired
    private CommcontentService commcontentService;
    @Autowired
    private UsercarService usercarService;
    @Autowired
    private UserstoreService userstoreService;
    @Autowired
    private CarsignService carsignService;
    @Autowired
    private TimeinfoService timeinfoService;
    @Autowired
    private CarsService carsService;

    @RequestMapping("/login")
    public List<UserAccount> Login(String account, String password){
        List<UserAccount> userAccount = null;
        if(account != null && password != null){
            userAccount = userAccountService.Login(account,password);
        }
        return userAccount;
    }

    @RequestMapping("/register")
    public Boolean Register(String account,String password,String name,String sex){
        Boolean checked = null;
        if(userAccountService.checkAccountExist(account)){
            checked = userAccountService.Register(account, password,name,sex);
        }else{
            checked = false;
        }

        return checked;
    }

    @RequestMapping("/modify")
    public Boolean modifyInfo(String name,String sex,int age,String phone,String address,String account){
        return userAccountService.modifyInfo(name,sex,age,phone,address,account);
    }

    @RequestMapping("/getcar")
    public List<Usercar> getCar(String name){
        return usercarService.getCar(name);
    }

    @RequestMapping("/car")
    public List<Recommentcar> Cartitle(String name){
        List<Recommentcar> recommentcars = recommentcarService.getAll(name);
        return recommentcars;
    }


    //上拉加载更多
    @RequestMapping("/findCarByPage")
    public List<Recommentcar> findCarByPage(int pageNow,int pageSize){
        Page<Recommentcar> page = new Page<>(pageNow,pageSize);
        QueryWrapper<Recommentcar> queryWrapper = new QueryWrapper<>();

        queryWrapper.orderByAsc("id");
        Page<Recommentcar> page1 = recommentcarService.getPage(page,queryWrapper);
        List<Recommentcar> list = page1.getRecords();
        return list;
    }

    @RequestMapping("/commguild")
    public List<Commguild> commGuild(){
        return commguildService.getAll();
    }

    @RequestMapping("/commcontent")
    public List<Commcontent> getAll(String name){
        return commcontentService.getAll(name);
    }

    @RequestMapping("/releasecontent")
    public Boolean ReleaseContent(String name,String content){
        return commcontentService.release(name, content);
    }

    @RequestMapping("/refreshcontent")
    public List<Commcontent> getByPagea(int pageNow,int pageSize){
        Page<Commcontent> page = new Page<>(pageNow,pageSize);
        QueryWrapper<Commcontent> queryWrapper = new QueryWrapper<>();

        queryWrapper.orderByDesc("id");
        Page<Commcontent> page2 = commcontentService.getPage(page,queryWrapper);
        List<Commcontent> list = page2.getRecords();
        return list;
    }

    @RequestMapping("/findContentByPage")
    public List<Commcontent> getByPage(int pageNow,int pageSize){
        Page<Commcontent> page = new Page<>(pageNow,pageSize);
        QueryWrapper<Commcontent> queryWrapper = new QueryWrapper<>();

        queryWrapper.orderByAsc("id");
        Page<Commcontent> page2 = commcontentService.getPage(page,queryWrapper);
        List<Commcontent> list = page2.getRecords();
        return list;
    }

    @RequestMapping("/getstore")
    public List<Userstore> getAllStore(){
        return userstoreService.getAll();
    }

    @RequestMapping("/getscar")
    public List<Carsign> gettAllCar(){
        return carsignService.getAll();
    }

    @RequestMapping("/getinfo")
    public List<Timeinfo> getAllInfo(String img1){
        return timeinfoService.getAll(img1);
    }

    @RequestMapping("/findInfoByPage")
    public List<Timeinfo> getInfoByPage(int pageNow,int pageSize){
        Page<Timeinfo> page = new Page<>(pageNow,pageSize);
        QueryWrapper<Timeinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");

        Page<Timeinfo> page1 = timeinfoService.getAll(page,queryWrapper);
        List<Timeinfo> list = page1.getRecords();
        return list;
    }



    @RequestMapping("/allCars")
    public List<Cars> getAllCate(String name){
        List<Cars> list = carsService.getCar(name);
        return list;
    }

}
